// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.NumberTo = cc.Class({
    name: 'cc.NumberTo',
    extends: cc.ActionInterval,

    ctor:function (duration, num) {
        this._startNum = 0;
        this._endNum = 1;
        this._deltaNum = 0;
        num !== undefined && cc.NumberTo.prototype.initWithDuration.call(this, duration, num);
    },

    /*
     * Initializes the action.
     * @param {Number} duration
     * @param {Number} progress
     * @return {Boolean}
     */
    initWithDuration:function (duration, num) { //function overload here
        if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
            this._endNum = num;
            return true;
        }
        return false;
    },

    clone:function () {
        var action = new cc.NumberTo();
        this._cloneDecoration(action);
        action.initWithDuration(this._duration, this._endNum);
        return action;
    },

    startWithTarget:function (target) {
        cc.ActionInterval.prototype.startWithTarget.call(this, target);

        var com = target.getComponent(cc.Label);
        if (com) {
            this._startNum = parseInt(com.string);
        }
        this._deltaNum = this._endNum - this._startNum;
    },

    update:function (dt) {
        dt = this._computeEaseTime(dt);
        if (this.target) {
            var com = this.target.getComponent(cc.Label);
            if (com) {
                com.string = Math.ceil(this._startNum + this._deltaNum * dt);
            } 
        }
    }
});

cc.numberTo = function (duration, num) { //function overload
    return new cc.NumberTo(duration, num);
};
